let app = getApp();
const WXAPI = require('../../utils/main')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0, //默认是待付款
    allOrderList: [], //所有
    database: true,
    identifier: true, //判断是否加载下一页
    orderType:'', //不传表示查询全部订单 0:待付款 1:待发货 2:已发货 
    page: 0, //页码
    pageSize: 10, //每页显示个数
    show2: false, //支付弹框
    balance:'',// 余额显示
    payType:'0',// 默认支付
    price:'',// 金额
    ordersn:'',// 订单号
    // orderTime:[],// 时间接口(已废弃)
    date_class:-1,// 日期高亮
    judge:false,
    orderTime:[
        {week:'星期一',date:'09-13'},
        {week:'星期二',date:'09-14'},
        {week:'星期三',date:'09-15'},
        {week:'星期四',date:'09-16'},
        {week:'星期五',date:'09-17'}
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      if(options.type){
        var type = parseInt(options.type)
        this.setData({
            active: type,
        });
        this.data.orderType = this.data.active;
      }
    this.allOrdersPsot();
    this.generate_order();
    this.order_time()
  },

  //时间接口
  order_time:function(){
    let that = this
    WXAPI.date({  
        _uid:wx.getStorageSync('uid'),
        _do:'select'
    }).then(function (res) {
        that.setData({
            // orderTime:res
        })
    }).catch(function (e) {
      wx.hideLoading()
      console.log(e)
    })
    },
  //切换状态
  toggleStatus:function (e) {
    console.log(e)
    let that = this
    let date = e.currentTarget.dataset.value

    that.allOrdersPsot(e.currentTarget.dataset.value)
    that.setData({
        date_class:e.currentTarget.dataset.index
    })
  },
  //订单接口
  allOrdersPsot:function(datetime){
    let that = this
    if(!datetime){
        datetime = ''
    }
    console.log(that.data.orderType)
    that.selectComponent("#loading").show();
    WXAPI.commodity_order({  
        _uid:wx.getStorageSync('uid'),
        _do:'select',
        _status:that.data.orderType,
        _date:datetime
    }).then(function (res) {
        that.selectComponent("#loading").hide()
        console.log(res)
        if (res.length == 0) {
            that.setData({
                identifier: false,
                database: false
            })
        }else{
            that.setData({
                allOrderList:res,
                database: true
            })

        } 
    }).catch(function (e) {
      console.log(e)
    })
  },
  // 顶部change事件
  onChange(e) {
    if (e.detail.title == "待付款") {
        this.setData({
            orderType: 0,
            page: 0,
            identifier: true,
            database: true,
            allOrderList: [],
        });
        this.allOrdersPsot();
        
    } else if (e.detail.title == "待发货") {
        this.setData({
            orderType: 1,
            page: 0,
            database: true,
            identifier: true,
            allOrderList: [],
            date_class:-1
        });
        this.allOrdersPsot();
    } else if (e.detail.title == "已发货") {
        this.setData({
            orderType: 2,
            page: 0,
            identifier: true,
            database: true,
            allOrderList: [],
        });
        this.allOrdersPsot();
    }else if (e.detail.title == "已完成") {
        this.setData({
            orderType: 3,
            page: 0,
            identifier: true,
            database: true,
            allOrderList: [],
        });
        this.allOrdersPsot();
    }
    },
      //更改订单状态
    async orderStatusBtn(e) {
        console.log(e)
        let textType = +e.currentTarget.dataset.type; //状态  1:取消订单 2：继续支付 3：确认订单 4：确认收货 
        let orderNmberId = +e.currentTarget.dataset.id; // 订单的id
        console.log(orderNmberId)
        let that = this
        if (textType == 1) {
            wx.showModal({
                title: '是否要取消订单',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: async(result) => {
                    if (result.confirm) {
                        WXAPI.commodity_order({ 
                            _uid:wx.getStorageSync('uid'), 
                            _id:orderNmberId,
                            _do:'delete'
                        }).then(function (res) {
                            if (res.res == 1) {
                                 wx.showToast({
                                    title: "取消成功",
                                    icon: "none",
                                });
                                that.onPullDownRefresh()
                            } else{
                                wx.showToast({
                                    title: "message",
                                    icon: "none",
                                });
                            }
                        }).catch(function (e) {
                          wx.hideLoading()
                          console.log(e)
                        })
                    }
                },
            });
        }else if (textType == 2) {
            console.log(e)
            this.setData({
                show2:true,
                price:e.target.dataset.price,
                ordersn:e.target.dataset.ordersn,
                payType:'0',
                judge:false
            })
        }else if(textType == 3){
            let result_s = {}
            let curGood = this.data.allOrderList.find(ele => {
                return ele.id == orderNmberId
              })
              result_s.orderNmberId = orderNmberId;
              result_s.thumb = curGood.thumb
              result_s.gprice = curGood.gprice
              result_s.title = curGood.title
            console.log(result_s)
            wx.showModal({
                title: '是否需要申请退款',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: async(result) => {
                    console.log(result)
                    if (result.confirm) {
                        wx.navigateTo({
                            url: "../refund/refund?result=" + JSON.stringify(result_s)
                        })
                    }
                },
              }); 
        }else if (textType == 4) {
            console.log(orderNmberId)
            wx.showModal({
                title: '是否确认收货',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: async(result) => {
                    if (result.confirm) {
                        WXAPI.commodity_order({  
                            _uid:wx.getStorageSync('uid'),
                            _do:"edit",
                            _id:orderNmberId,
                            _status:3
                        }).then(function (res) {
                            console.log(res)
                            if (res.res == 1) {
                                 wx.showToast({
                                    title: "已确认收货",
                                    icon: "none",
                                });
                                that.onPullDownRefresh()
                            } else{
                                wx.showToast({
                                    title: "message",
                                    icon: "none",
                                });
                            }
                        }).catch(function (e) {
                          wx.hideLoading()
                          console.log(e)
                        })
                    }
                },
            });
        }else if (textType == 5){
            wx.navigateTo({
                url: "../comments/comments"
            })
        }
    },  
    //返回参数
    signEvent: function(e){
        console.log(e)
        this.setData({
            show2:false
        })
        this.onPullDownRefresh();
    },
    
    //查询支付余额
    generate_order:function(jihe){
        let that = this
        WXAPI.commodity_order({  
          _uid:wx.getStorageSync('uid'),
          _do:'pay'
        }).then(function (res) {
          if(res.res === 1){
            that.setData({
              balance:res.user_money,
              payType:0
            });
          }
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //返回刷新
  changeData:function(){
    this.onPullDownRefresh()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    that.selectComponent("#loading").show();
    that.setData({
        'allOrderList': []
    })
    // this.setData({
    //     page: 0,
    //     identifier: true
    // })
    setTimeout(() => {　　　　　　　　
        that.allOrdersPsot();　
        wx.stopPullDownRefresh(); //停止下拉刷新
    }, 1000);
  },
  //关闭支付框

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})